'use strict'
import { QueryInterface, DataTypes } from 'sequelize'
/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
    await queryInterface.createTable('Categories', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER.UNSIGNED,
      },
      name: {
        allowNull: false,
        type: Sequelize.STRING,
      },
      parentId: {
        type: Sequelize.INTEGER.UNSIGNED,
        // references: {
        //   model: 'Categories',
        //   key: 'id',
        // },
        // onDelete: 'RESTRICT',
        // onUpdate: 'CASCADE', // 默认RESTRICT
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
    })
    await queryInterface.addIndex('Categories', {
      fields: ['parentId'],
    })
  },
  async down(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
    await queryInterface.dropTable('Categories')
  },
}
